<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>new methods</title>
</head>
<body>
<button id="download">点击下载</button>

<script>
    const isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    const isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
    const downloadFile = function (sUrl) {

        //iOS devices do not support downloading. We have to inform user about this.
        if (/(iP)/g.test(navigator.userAgent)) {
            return false;
        }

        //If in Chrome or Safari - download via virtual link click
        if (isChrome || isSafari) {
            //Creating new link node.
            var link = document.createElement('a');
            link.href = sUrl;
            if (link.download !== undefined) {
                //Set HTML5 download attribute. This will prevent file from opening if supported.
                var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
                link.download = fileName;
            }

            //Dispatching click event.
            if (document.createEvent) {
                console.log('run document.createEven ')
                var e = document.createEvent('MouseEvents');
                e.initEvent('click', true, true);
                link.dispatchEvent(e);
                return true;
            }
        }

        // Force file download (whether supported by server).
        if (sUrl.indexOf('?') === -1) {
            sUrl += '?download';
        }

        console.log('run window.open')
        window.open(sUrl, '_self');
        return true;
    }
    // 在 ie11 下无法下载图/文件，只会导航至路径 / todo
</script>
<script>
    const dlbtn = document.querySelector('#download')
    dlbtn.onclick = function () {
        // downloadFile('./tt.jpg') // download success

        const iframe = document.createElement('iframe');
        iframe.src = './1.PNG'
        iframe.id = 'saveFile'
        iframe.style.display = 'none'
        iframe.onload = function () {
            document.frames["saveFile"].document.execCommand('saveAs')
            iframe.removeNode(true)
        }
        document.body.appendChild(iframe)
    };

    // function browserIsIe() {
    //     if (!!window.ActiveXObject || "ActiveXObject" in window)
    //         return true;
    //     else
    //         return false;
    // }
</script>
</body>
</html>
